using System;
using System.IO;
using System.Windows.Media;

namespace Intuipic.Data
{
	/// <summary>
	/// A base class for all informational items that operate against an instance of <see cref="DirectoryInfo"/>.
	/// </summary>
	public abstract class DirectoryInformationBase : InformationBase
	{
		private readonly DirectoryInfo _directoryInfo;
		
		/// <summary>
		/// Constructs an instance of <c>DirectoryInformationBase</c> with the specified details.
		/// </summary>
		protected DirectoryInformationBase(ImageSource closedIcon, ImageSource openIcon, InformationBase parent, DirectoryInfo directoryInfo)
			: base(directoryInfo.Name, directoryInfo.FullName, closedIcon, openIcon, parent)
		{
			_directoryInfo = directoryInfo;
		}

		/// <summary>
		/// Loads the children of this directory informational item.
		/// </summary>
		protected override void LoadChildrenCore()
		{
			try
			{
				foreach (DirectoryInfo directoryInfo in _directoryInfo.GetDirectories())
				{
					Children.Add(new DirectoryInformation(this, directoryInfo));
				}
			}
			catch (UnauthorizedAccessException)
			{
				//swallow - no access to directory so we can't enumerate children
			}
		}
	}
}
